package com.lzw.controller;

import com.alibaba.fastjson.JSONObject;
import com.lzw.bean.Auth;
import com.lzw.bean.Role;
import com.lzw.bean.User;
import com.lzw.bean.result.LoginResult;
import com.lzw.bean.result.Meta;
import com.lzw.bean.result.ResultObject;
import com.lzw.service.AuthService;
import com.lzw.service.LoginService;
import com.lzw.service.RoleService;
import com.lzw.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @auther: lzw
 * @date: 2021/9/25 - 09 - 25 - 20:26
 * @Description: com.lzw.controller
 */

@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthService authService;

    @RequestMapping("/auth/login")
    public JSONObject login(@RequestBody Map params){
        String username = (String) params.get("username");
        String password = (String) params.get("password");
        User user = loginService.login(username, password);
        System.out.println(user);
        Meta meta = null;
        LoginResult loginResult = null;
        if (user != null){
            String token = JwtUtils.createToken(user.getRid().toString());
            meta = new Meta("登录成功",200);
            loginResult = new LoginResult();
            loginResult.setId(user.getId());
            loginResult.setRid(user.getRid());
            loginResult.setMobile(user.getMobile());
            loginResult.setUsername(user.getUsername());
            loginResult.setToken(token);
        }else {
            meta = new Meta("未找到用户",400);
        }
        ResultObject resultObject = new ResultObject(meta, loginResult);
        String s = JSONObject.toJSONString(resultObject);
        System.out.println(s);
        return JSONObject.parseObject(JSONObject.toJSONString(resultObject));
    }

    @RequestMapping("auth/menus")
    public JSONObject menu(HttpServletRequest req){
        String token = req.getHeader("Auth");
        Integer rid = JwtUtils.vaildToken(token);
        System.out.println(rid);
        Role role = roleService.getRole(rid);
        System.out.println(role);
        Meta meta = null;
        List<Auth> authList = new ArrayList<>();
        if (role!=null){
            String[] authIdList = role.getAuth().split("-");
            for (String s : authIdList) {
                System.out.println(s);
                Auth authObj = authService.getAuthObj(Integer.valueOf(s));
                System.out.println(authObj);
                authList.add(authObj);
            }
            meta = new Meta("登录成功",200);
        }else {
            meta = new Meta("未找到用户",400);
        }
        ResultObject resultObject = new ResultObject();
        resultObject.setMeta(meta);
        resultObject.setData(authList);
        return JSONObject.parseObject(JSONObject.toJSONString(resultObject));
    }
}
